package com.iespring.controller.admin.sys;

import com.iespring.bean.admin.sys.bo.*;
import com.iespring.bean.admin.sys.po.SysRolePo;
import com.iespring.bean.admin.sys.po.SysStoragePo;
import com.iespring.bean.admin.sys.vo.*;
import com.iespring.bean.common.BasePageInfo;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.bean.common.CommonData;
import com.iespring.service.admin.SysService;
import com.iespring.util.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

/**
 * 后台系统管理
 * @return
 * @author 杨宇健
 * @since 2022/12/28 17:29
 */

@RestController
@RequestMapping("admin")
public class SystemController {
    @Autowired
    SysService sysService;

    @RequestMapping("admin/list")
    public BaseRespVo adminList(BasePageInfo basePageInfo){
        CommonData commonData = sysService.AdminList(basePageInfo);
        BaseRespVo respVo = BaseRespVo.ok(commonData);
        return respVo;
    }



    @RequestMapping("admin/create")
    public BaseRespVo adminCreate(@RequestBody @Valid SysAdminCreateBo adminBo, BindingResult bindingResult){
        ValidationUtils.validParam(bindingResult);
        SysAdminCreateVo admin = sysService.createAdmin(adminBo);
        if (admin == null) {
            return BaseRespVo.error("插入失败，请检查名字", 500);
        }
        return BaseRespVo.ok(admin);
    }

    @RequestMapping("admin/update")
    public BaseRespVo adminUpdate(@RequestBody @Valid SysAdminCreateBo adminBo, BindingResult bindingResult){
        ValidationUtils.validParam(bindingResult);
        SysAdminUpdateVo admin = sysService.updateAdmin(adminBo);
        if (admin == null) {
            return BaseRespVo.error("更新失败，请联系管理员", 500);
        }
        return BaseRespVo.ok(admin);
    }

    @RequestMapping("admin/delete")
    public BaseRespVo adminDelete(@RequestBody SysAdminCreateBo adminBo){
        Integer affect = sysService.deleteAdmin(adminBo);
        if (affect == 0) {
            return BaseRespVo.error("删除失败，请联系管理员", 500);
        }
        return BaseRespVo.ok();
    }


    @RequestMapping("role/list")
    public BaseRespVo roleOptions(SysRolePageBo basePageInfo){

        CommonData commonData = sysService.roleOptions(basePageInfo);
        BaseRespVo respVo = BaseRespVo.ok(commonData);
        return respVo;
    }

    @RequestMapping("role/options")
    public BaseRespVo roleOptionsPage(){
        CommonData commonData = sysService.roleOptions();
        BaseRespVo respVo = BaseRespVo.ok(commonData);
        return respVo;
    }

    @RequestMapping("role/create")
    public BaseRespVo roleCreate(@RequestBody SysRoleCreateBo roleBo){
        //String remoteAddr = request.getRemoteAddr();
        //return null;
        SysRoleCreateVo role = sysService.createRole(roleBo);
        if (role == null) {
            return BaseRespVo.error("插入失败，请检查名字", 500);
        }
        return BaseRespVo.ok(role);
    }

    @RequestMapping("role/delete")
    public BaseRespVo roleDelete(@RequestBody SysRolePo role){
        int affect = sysService.deleteRole(role);
        if (affect == 0) {
            return BaseRespVo.error("删除失败，请联系管理员", 500);
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("role/update")
    public BaseRespVo roleUpdate(@RequestBody @Valid SysRolePo role, BindingResult bindingResult){

        ValidationUtils.validParam(bindingResult);

        int affect = sysService.updateRole(role);
        if (affect == 0) {
            return BaseRespVo.error("删除失败，请联系管理员", 500);
        }
        return BaseRespVo.ok();
    }
    @RequestMapping(value = "role/permissions", method = RequestMethod.GET)
    public BaseRespVo rolepermissions(Integer roleId){
        SysPermissionVo permission = sysService.permission(roleId);

        if (permission == null) {
            return BaseRespVo.error("删除失败，请联系管理员", 500);
        }
        return BaseRespVo.ok(permission);
    }

    /**
     * 权限增加
     * @param
     * @return com.iespring.bean.common.BaseRespVo
     * @author 杨宇健
     * @since 2022/12/29 22:44
     */
    @RequestMapping(value = "role/permissions", method = RequestMethod.POST)
    public BaseRespVo rolepermissionsPost(@RequestBody SysRolePermissionBo rolePermission){
        Integer code = sysService.permissionPost(rolePermission);

        if (code != 200) {
            return BaseRespVo.error("添加权限失败，请联系管理员", 500);
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("storage/create")
    public BaseRespVo storageCreate(MultipartFile file) throws IOException {
        BaseRespVo respVo = sysService.storage(file);


        return respVo;
    }

    @GetMapping("storage/list")
    public BaseRespVo storageList(SysStoragePageVo basePageInfo){
        CommonData commonData = sysService.storagePage(basePageInfo);
        BaseRespVo respVo = BaseRespVo.ok(commonData);
        return respVo;
    }

    @PostMapping("storage/update")
    public BaseRespVo storageUpdate(@RequestBody SysStoragePo storagePo){
        BaseRespVo respVo = sysService.storageUpdate(storagePo);
        //BaseRespVo respVo = BaseRespVo.ok(commonData);
        return respVo;
    }
    @PostMapping("storage/delete")
    public BaseRespVo storageDelete(@RequestBody SysStoragePo storagePo){
        BaseRespVo respVo = sysService.storageDelete(storagePo);
        //BaseRespVo respVo = BaseRespVo.ok(commonData);
        return respVo;
    }
    @GetMapping("log/list")
    public BaseRespVo logList(BasePageInfo pageInfo) {

        CommonData commonData = sysService.logPage(pageInfo);
        BaseRespVo respVo = BaseRespVo.ok(commonData);
        return respVo;
    }
}
